package com.markerhub.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class KaptchaService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public Map<String,Object> createToken(String captcha){

        //生成一个token
        String cToken = UUID.randomUUID().toString();

        //把cToken和正确的验证码保存进redis
        stringRedisTemplate.opsForValue().set(cToken,captcha);
        //设置过期时间为120秒
        stringRedisTemplate.expire(cToken, 120, TimeUnit.SECONDS);
        Map<String, Object> map = new HashMap<>();
        map.put("cToken", cToken);
        System.out.println("cToken:" + cToken);
        System.out.println("captcha:" + captcha);
        return map;
    }
}
